<?php
/**
 * Created by PhpStorm.
 * User: zk
 * Date: 2018/9/29
 * Time: 下午3:20
 */

namespace app\models;
use yii\db\ActiveRecord;

class Bar_bar extends ActiveRecord {
    //新建酒吧
    public function addBar($barName, $cityCode, $barAddress, $businessHour, $barType, $barService, $barPhotos ,$barLogo, $barNotice, $barHeadline, $barHeadlineUrl, $barRecommend) {

    }

    public function getBars(){
        $result = Bar_bar::find()->limit(100)->asArray()->all();
        return $result;
    }

    //获取商家列表
    public function getBarList($limit, $offset, $cityCode, $status, $barType, $barName, $startTime, $endTime,$barClaim,$barSort) {
        $bar = Bar_bar::find();
        if ($cityCode != -1)
            $bar->andWhere(['bar_bar.city_code' => $cityCode]);
        if ($status != -1)
            $bar->andWhere(['is_deleted' => $status]);
        if ($barType != -1)
            $bar->andWhere(['bar_type' => $barType]);
        if ($barName != -1)
            $bar->andWhere(['like', 'bar_name', $barName]);
        if ($startTime != -1) {
            $bar->andWhere(['between', 'gmt_create', $startTime, $endTime]);
        }
        if($barClaim != -1)
            $bar->andWhere(['is_claim' => $barClaim]);

        $count = $bar->count();

        if($barClaim == 0)
            $bar->orderBy('gmt_create DESC');
        else if($barSort == 1)
            $bar->orderBy('people_count DESC');
        else if($barSort == 2)
            $bar->orderBy('dynamic_count DESC');
        else
            $bar->orderBy('bar_id DESC');

        $result = $bar->joinWith('city')->limit($limit)->offset($offset)->asArray()->all();
        return ['total' => $count, 'rows' => $result];
    }

    //编辑酒吧
    public function editBar($bar) {
        $bar = Bar_bar::findOne(['bar_id' => $bar['bar_id']]);
        if (isset($bar['bar_name']))
            $bar->bar_name = $bar['bar_name'];
        if (isset($bar['city_code']))
            $bar->city_code = $bar['city_code'];
        if (isset($bar['bar_address']))
            $bar->bar_address = $bar['bar_address'];
        if (isset($bar['bar_address']))
            $bar->bar_address = $bar['bar_address'];
        if (isset($bar['business_hours']))
            $bar->business_hours = $bar['business_hours'];
        if (isset($bar['bar_type']))
            $bar->bar_type = $bar['bar_type'];
        if (isset($bar['bar_server']))
            $bar->bar_server = $bar['bar_server'];

        if (isset($requestPut['bar_photos'])) {
            $bar->bar_photos = $requestPut['bar_photos'];
        }
        if (isset($requestPut['bar_logo'])) {
            $bar->bar_logo = $requestPut['bar_logo'];
        }
        if (isset($requestPut['bar_notice'])) {
            $bar->bar_notice = $requestPut['bar_notice'];
        }
        if (isset($requestPut['bar_headline'])) {
            $bar->bar_headline = $requestPut['bar_headline'];
        }
        if (isset($requestPut['bar_recommend'])) {
            $bar->bar_recommend = $requestPut['bar_recommend'];
        }
        if (isset($requestPut['headline_url'])) {
            $bar->headline_url = $requestPut['headline_url'];
        }

        if (isset($bar['status']))
            $bar->is_deleted = $bar['status'];
        if (isset($bar['weight_value']))
            $bar->weight_value = $bar['weight_value'];
        $bar->gmt_modified=date('Y-m-d H:i:s',time());
        return $bar->save();
    }

    public function getCity(){
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(Gl_city::className(), ['city_code' => 'city_code']);
    }
}